package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageList;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.EmployeeQuery;
import cn.itsource.pethome.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: ${file_name}
 * @Package:${package_name}
 * @Description:(作用)
 * @author:chenhang
 * @date:${date}
 * @version:V1.0
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /*
    * 添加部门
    * */
    //@RequestMapping(value = "/save",method = RequestMethod.PUT)
    @PutMapping
    public AjaxResult save(@RequestBody Employee employee){
        try {
            employeeService.save(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"添加失败：" + e.getMessage());
        }
    }
    /*
    * 修改部门*/
    //@RequestMapping(value = "/update",method = RequestMethod.POST)
    @PostMapping
    public AjaxResult update(@RequestBody Employee employee){
        try {
            employeeService.update(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"修改失败：" + e.getMessage());
        }
    }
    /*
     * 删除部门*/
    //@RequestMapping(value = "/update",method = RequestMethod.POST)
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败：" + e.getMessage());
        }
    }
    /*批量删除*/
    @PatchMapping("/patchDelete")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败：" + e.getMessage());
        }
    }
    /*
    * 根据id查询单个部门*/
    //@RequestMapping(value = "/findById",method = RequestMethod.GET)
    @GetMapping("{id}")
    public Employee findById(@PathVariable("id") Long id) {
       return employeeService.findById(id);
    }
    /*
    * 分页查询部门*/
    //@RequestMapping(value = "/findAll",method = RequestMethod.PATCH)
    @PatchMapping
    public PageList<Employee> findPageByQuery(@RequestBody EmployeeQuery query) {
       return employeeService.findPageByQuery(query);
    }
}
